<template>
  <div style="margin: 20px">
    <el-row :gutter="20">
      <el-col :span="12">
        <div class="A1">
          <div>
            <el-row>
              <el-col :span="8">
                <el-input
                  placeholder="请输入内容"
                  prefix-icon="el-icon-search"
                  v-model="listQuery.name"
                  size="mini"
                >
                </el-input
              ></el-col>
              <el-col :span="16">
                <el-button type="primary" @click="chax" size="mini"
                  ><i class="el-icon-search"></i>搜索</el-button
                >
                <el-button type="primary" @click="chongzhi" size="mini"
                  ><i class="el-icon-search"></i>重置</el-button
                >
              </el-col> </el-row
            ><br />
            <el-row>
              <el-col :span="18">
                <el-button
                  type="primary"
                  @click="dialogFormVisible = true"
                  size="mini"
                  >添加<i class="el-icon-upload el-icon--right"></i
                ></el-button>
                <el-dialog title="新增字典" :visible.sync="dialogFormVisible">
                  <el-form :model="dictform">
                    <el-form-item label="名称" :label-width="formLabelWidth">
                      <el-input
                        v-model="dictform.name"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="描述" :label-width="formLabelWidth">
                      <el-input
                        v-model="dictform.description"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible1 = false"
                      >取 消</el-button
                    >
                    <el-button type="primary" @click="bianji">确 定</el-button>
                  </div>
                </el-dialog>
                <el-button type="primary" @click="Batchdelete" size="mini"
                  >批量删除<i class="el-icon-delete"></i
                ></el-button>
              </el-col>
              <el-col :span="6">
                <el-button-group>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                  ></el-button>
                  <el-button
                    type="primary"
                    icon="el-icon-refresh"
                    size="mini"
                    @click="shuaxing"
                  ></el-button>
                  <el-button
                    type="primary"
                    icon="el-icon-s-grid"
                    size="mini"
                  ></el-button>
                </el-button-group>
              </el-col>
            </el-row>
          </div>
          <br /><br />
          <el-table
            :data="list"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            @row-click="chax1"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column
              label="名称"
              width="270px"
              style="margin-left: 30px"
            >
              <template slot-scope="scope">
                <span style="margin-left: 0px">{{ scope.row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="描述"
              width="270px"
              style="margin-left: 10px"
            >
              <template slot-scope="scope">
                <span style="margin-left: 0px">{{
                  scope.row.description
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150px">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.row)"
                  style="background-color: rgb(98, 166, 243)"
                  ><i class="el-icon-edit" style="color: rgb(255, 255, 255)"></i
                ></el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  ><i class="el-icon-delete"></i
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :page-size="listQuery.pageSize"
              :page-sizes="[2, 4, 8]"
              :current-page.sync="listQuery.pageNum"
              :total="total"
            >
            </el-pagination>
          </div>
        </div>
      </el-col>

      <el-col :span="12">
        <div class="A1">
          <dictdetail ref="sonref"></dictdetail>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {
  dictgetlist,
  dictdelet,
  dictinsert,
  dictupdate,
  dictdetailDeleteall,
  dictpldelet,
} from "@/api/dict";
import Dictdetail from "./dictdetail";
export default {
  components: {
    Dictdetail,
  },
  data() {
    return {
      operateType: null,
      listQuery: {
        keyword: null,
        nameCn: null,
        pageNum: 1,
        pageSize: 4,
        name: null,
      },
      list: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      lookup: null,
      cklook: false,
      dictvalue: null,
      dictform: {
        id: null,
        name: null,
        description: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      tdictdescription: null,
      aa: null,
      dictlist: null,
    };
  },
  created() {
    this.chax();
  },
  methods: {
    chax() {
      //查询分页
      this.listLoading = true;
      console.log("----------------2222221--------------1");
      dictgetlist(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
        this.totalPage = response.data.pages;
        this.pageSize = response.data.size;
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.chax();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.chax();
    },
    chax1(row) {
      //点击行触发事件
      this.dictlist = row;

      this.$bus.$emit("row", this.dictlist);//向子组件传递参数
    },

    handleEdit(row) {
      //第一个编辑
      this.dictform = row;
      this.dialogFormVisible = true;
    },

    chongzhi() {//搜索框重置
      this.listQuery.name = null;
      this.chax();
    },
    handleDelete(id) {
      //单个删除
      this.$confirm("确定删除他吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        dictdelet(id).then((response) => {
          this.$message({
            message: "已经删除成功了哟！",
            type: "success",
            duration: 1000,
          });
          this.chax();
          this.shuaxing();
        });
      }),
        dictdetailDeleteall(id).then((response) => {});//删除dict表的同时删除另一个表相关联的数据
    },

    handleSelectionChange(val) {
      //批量删除获取id
      this.multipleSelection = val;
    },
    Batchdelete() {
      //批量删除
      var arr = [];
      for (var a = 0; a < this.multipleSelection.length; a++) {
        arr.push(this.multipleSelection[a].infoId);
      }
      dictpldelet(arr).then((response) => {
        this.chax();
      });
    },

    bianji() {//添加和修改判断
      if (this.dictform.id != null) {
        this.xiugai();
      } else {
        this.tianj();
      }
    },

    qingchu() {//格式化弹出框数据
      (this.dictform.id = null),
        (this.dictform.name = null),
        (this.dictform.description = null),
        (this.dictform.createBy = null),
        (this.dictform.updateBy = null),
        (this.dictform.createTime = null),
        (this.dictform.updateTime = null);
    },

    xiugai() {
      //修改
      dictupdate(this.dictform).then((response) => {
        this.dialogFormVisible = false;
        this.$message({
          message: "修改成功！",
          type: "success",
          duration: 1000,
        });
        this.qingchu();
        this.chax();
      });
    },

    tianj() {
      //添加
      dictinsert(this.dictform).then((response) => {
        this.dialogFormVisible = false;
        this.$message({
          message: "添加成功！",
          type: "success",
          duration: 1000,
        });
        this.qingchu();
        this.chax();
      });
    },

    // shuaxing() {
    //   this.$router.push({ path: "./tiao" });
    // },
  },
};
</script>
<style >
.A1 {
  width: 100%;
  height: 100%;
  border: 1px solid rgb(234, 229, 229);
  border-radius: 10px;
  box-shadow: 1px 1px 20px rgb(230, 223, 223);
  padding: 13px;
}
</style>